#include "StdAfx.h"
#include "IDilate.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const IDilate::FunctionNameIDilate = "Dilate";

IDilate::IDilate(void) 
{
	_fnApi.SetFunctionName(IDilate::FunctionNameIDilate);
	_fnApi.SetFunctionDescription("Dilates an image by using a specific structuring element");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefString(0, "Element", "The structuring element used for dilation. If element=Mat() , a 3\times 3 rectangular structuring element is used", ProcParamDef::input, 0, 500 ));
	_fnApi.AddParamDef(new ProcParamDefInt(1, "AnchorX", "Position of the anchor within the element. The default value -1 means that the anchor is at the element center", ProcParamDef::input, -500, 500 ));
	_fnApi.AddParamDef(new ProcParamDefInt(2, "AnchorY", "Position of the anchor within the element. The default value -1 means that the anchor is at the element center", ProcParamDef::input, -500, 500 ));
	_fnApi.AddParamDef(new ProcParamDefInt(3, "Iterations", "The number of times dilation is applied", ProcParamDef::input, 0, 500 ));
	//_fnApi.AddParamDef(new ProcParamDefString(4, "BorderType", "The pixel extrapolation method", ProcParamDef::input, 0, 500 ));
	//_fnApi.AddParamDef(new ProcParamDefString(5, "borderValue", "The border value in case of a constant border.", ProcParamDef::input, 0, 500 ));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


IDilate::~IDilate(void)
{
	
}


bool IDilate::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		ProcParamString *element = dynamic_cast<ProcParamString *>(step->GetInputs()->GetParam(0));	//Check the Param number
		ProcParamInt *AnchorX = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(1));
		ProcParamInt *AnchorY = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(2));
		ProcParamInt *Iteration = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(3));

		//ProcParamInt *result = dynamic_cast<ProcParamInt *>(step->GetOutputs()->GetParam(0));		//Check the type and check the DefType
		if  (element != '\0' && AnchorX != '\0' && AnchorY != '\0' && Iteration != '\0')
		{
			cv::Mat output;
			cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());
			cv::Mat melement = cv::imread(element->GetValueStr());
						
			cv::dilate(input, output, melement, cv::Point(int(AnchorX->GetValue()),int(AnchorY->GetValue())), int(Iteration->GetValue()));

			cv::imwrite(step->GetIntermediateImagePathFilename(), output);

			input.release();
			melement.release();
			//result->SetValue(0);
			rc = true;
		}
	}
	return rc;
}
